﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Advanced_GetCallback_Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void leetSocket1_OnReceiveCompletedDataEVENT(object value, byte[] bArray)
        {
            byte[] b = (byte[])value;

            MessageBox.Show("Server: I received a byte array with length: " + b.Length.ToString() + " (" + (b.Length / (1024 * 1024)).ToString() + "MB)");
        }

        private void leetSocket1_OnReceiveChunkEVENT(byte[] CustomHeader, ulong TotalLength, int ReceivedBytes, ulong ReceivedBytesAddition, bool isFinished, int CommandID)
        {
            //invoke is required because the socket system works in a separate thread. 
            //Therefore invoking this method in gui thread with the help of the method invoke,
            //allows us to manipulate gui objects, see for further explanation msdn, or google
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MethodInvoker(delegate() { leetSocket1_OnReceiveChunkEVENT(CustomHeader, TotalLength,  ReceivedBytes,  ReceivedBytesAddition,  isFinished,  CommandID); }));
                return;
            }

            //for reseting the bar (received and receivedaddition are the same for thre first datachunk)
            if (Convert.ToUInt64(ReceivedBytes) == ReceivedBytesAddition) {
                progressBar1.Value = 0; 
            }

            progressBar1.Value = (int)((ReceivedBytesAddition * 100) / TotalLength);                        
        }
    }
}
